<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>symbol的额外补充</title>
</head>
<body>

  <script>

    // 1. Symbol的创建
    // 1.1 创建Symbol
    let s1 = Symbol('atguigu');
    let s2 = Symbol('atguigu');
    console.log(s1 === s2); // false

    // 1.2 Symbol.for()
    let s3 = Symbol.for('atguigu');
    let s4 = Symbol.for('atguigu');
    console.log(s3 === s4); // true

    // 1.3 Symbol.keyFor()
    let s5 = Symbol.for('atguigu'); // 1.3.1 先用Symbol.for()创建一个Symbol

    // 2. Symbol的属性描述符
    const sl1 = Symbol()
    const sl2 = Symbol()
    const obj = {
      [sl1]: 'hello',
      [sl2]: 'world',
      age: 18,
      sex: '男'
    }

    // 2.1 Object.getOwnPropertySymbols(obj) 获取对象中所有Symbol属性
    const symbols = Object.getOwnPropertySymbols(obj)
    console.log(symbols); // [ Symbol(), Symbol() ]

    // 2.2 Reflect.ownKeys(obj) 获取对象中所有属性
    console.log(Reflect.ownKeys(obj)); // [ Symbol(), Symbol(), 'age', 'sex' ]

    // 2.3 for of 获取对象中所有Symbol属性
    for (const item of symbols) {
      console.log(obj[item]);
    }

    // 2.4 keyFor 获取对象中所有Symbol属性
    console.log(Symbol.keyFor(sl1)); // undefined
    console.log(Symbol.keyFor(sl2)); // undefined
    console.log(Symbol.keyFor(s5)); // atguigu


  </script>
  
</body>
</html>